package jihyun51.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import jihyun51.dao.ZipcodeDAO;

public class ZipcodeSub extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField dong;
	private JTextField etcAddr;
 private JComboBox baseAddr= new JComboBox() ;
 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ZipcodeSub dialog = new ZipcodeSub(null,null,null,null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
//	public ZipcodeSub() {
		public ZipcodeSub(final JTextField Zip_1,final JTextField Zip_2,final JTextField txt_Addr,final JTextField txt_Addr2) {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel txtDong = new JLabel("동입력");
		dong = new JTextField();
		dong.setColumns(10);
		JButton findBtn = new JButton(" 주소검색 ");
		findBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//동을 입력하지 않고 검색버튼을 누른 경우 경고창 표시
				if (dong.getText().equals("")){
					JOptionPane.showMessageDialog(null, " 동을 입력하세요.");
					return;
				}
				
//				String[] addr=new String[] {
//						"123-456 서울시 광진구 자양동 789번지",
//						"234-567 부산시 남구 문현2동 12번지",
//						"345-678 대구시 대구 대구동 35번지"
//				};
				
				//zipcode테이블에서 동으로 검색한 결과를 ArrayList 객체에 담아주면
				//JComboBox 객체에 목록으로 추가
				//String[] 배열로 다시 작성
				
				ArrayList<String> zip =ZipcodeDAO.findzip(dong.getText());
				
//				ArrayList에 담긴 내용을 배열로 옯겨 담음
				String[] addr=new String[zip.size()];
				for(int i=0; i<zip.size(); ++i){
					addr[i] = zip.get(i);
				}
				
				
//				baseAddr = new JComboBox(addr);
//				baseAddr.repaint(); // 사용불가 ㅠ_-
				//콤보박스에 목록을 변경하려면 기존 목록을 모두 지우고 난후 addItem 메서드로 새로 추가해야 함.
				baseAddr.removeAllItems(); // 기존목록제거
				for (String l : addr ){
					baseAddr.addItem(l); //목록새로추가
				}
				
			}
		});
		
		
		etcAddr = new JTextField();
		etcAddr.setColumns(10);
		
		JButton sendBtn = new JButton(" 선택 후 닫기");
		sendBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//주소검색 후 적절한 우편번호 및 주소를 선택하고 
				// 나머지 주소를 입력한 후 ZipcodeMain의 zip1,zip2,addr,etcAddr에
				//자동으로 입력하고 현재 창 닫음.
			String  tmp= (String) baseAddr.getSelectedItem();
/*			String zip1=tmp.split(" ")[0].split("-")[0];
			String zip2=tmp.split(" ")[0].split("-")[1];
			String addr=tmp.substring(7);*/
			String zip1=tmp.split(" ")[0].substring(0, 2);
			String zip2=tmp.split(" ")[0].substring(3, 6);
			String addr=tmp.substring(7);
//			ZipCodeMain의 Zip_1, Zip_2,txt_Addr, txt_Addr2에 사용자가 선택하고 입력한 값 전달.
			Zip_1.setText(zip1);
			Zip_2.setText(zip2);
			txt_Addr.setText(addr);
			txt_Addr2.setText(etcAddr.getText());
			//창 닫기
			dispose();
			
		}
		});
		
		JButton notBtn = new JButton(" 검색 취소 ");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(57)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(29)
							.addComponent(sendBtn)
							.addGap(32)
							.addComponent(notBtn))
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
							.addComponent(etcAddr, Alignment.TRAILING)
							.addComponent(baseAddr, Alignment.TRAILING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(txtDong)
								.addGap(28)
								.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(findBtn))))
					.addContainerGap(86, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(49)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtDong)
						.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(findBtn))
					.addGap(18)
					.addComponent(baseAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(etcAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(sendBtn)
						.addComponent(notBtn))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
